package com.edu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.eduservice.entity.EduSubject;
import com.edu.eduservice.entity.excel.SubjectData;
import com.edu.eduservice.listener.SubjectExcelListener;
import com.edu.eduservice.mapper.EduSubjectMapper;
import com.edu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.eduservice.vo.subjectTreeVo;
import com.edu.eduservice.vo.subjectVo;
import com.edu.eduservicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    /**
     * 添加课程分类
     * @param file
     */
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {

        //创建实体类 和 excel对应
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream,SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20002,"添加课程分类失败");
        }


    }

    /***
     * 二级树形 目录
     * @return
     */
    @Override
    public List<subjectTreeVo> SubjectList() {
        //最终数组
        ArrayList<subjectTreeVo> list =new ArrayList<>();
        //获取一级分类记录
        QueryWrapper<EduSubject> wrapper = new QueryWrapper();
        wrapper.eq("parent_id",0);
        wrapper.orderByAsc("sort","id");
        List<EduSubject> eduSubjects = baseMapper.selectList(wrapper);

        //获取二级分类记录
        QueryWrapper<EduSubject> wrapper1 =new QueryWrapper<>();
        wrapper1.ne("parent_id",0);
        wrapper1.orderByAsc("sort","id");
        List<EduSubject> eduSubjects1 = baseMapper.selectList(wrapper1);

        //填充一级分类vo数据
        int size = eduSubjects.size();
        for (int i = 0; i < size; i++) {
            EduSubject eduSubject = eduSubjects.get(i);

            //创建一级类别vo对象

            subjectTreeVo subjectTreeVo = new subjectTreeVo();
            BeanUtils.copyProperties(eduSubject,subjectTreeVo);
            list.add(subjectTreeVo);

            //填充二级分类vo数据
            ArrayList<subjectVo> subjectVos = new ArrayList<>();
            int size1 = eduSubjects1.size();
            for (int j = 0; j < size1; j++) {

                EduSubject eduSubject1 = eduSubjects1.get(j);
                if (eduSubject.getId().equals(eduSubject1.getParentId())){

                    //创建二级类别
                    subjectVo subjectVo = new subjectVo();
                    BeanUtils.copyProperties(eduSubject1,subjectVo);
                    subjectVos.add(subjectVo);

                }
            }
            subjectTreeVo.setChildren(subjectVos);

        }


        return list;
    }
}
